import { createRouter, createWebHashHistory } from "vue-router";
import Home from "@/views/Home.vue";
import NotFound from "@/views/NotFound.vue";
// createRouter 创建路由对象
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/home/:id",
      component: Home,
      beforeEnter(to, from, next) {
        console.log("beforeEnter");
        next();
      },
    },
    {
      path: "/:p(.*)",
      component: NotFound,
    },
  ],
});

router.beforeEach((to, from, next) => {
  console.log("beforeEach");
  next();
});

export default router;

/*
路由守卫执行顺序
onBeforeRouteLeave->beforeEach->beforeEnter->beforeResolve->afterEach

beforeEach->onBeforeRouteUpdate->beforeResolve->afterEach
*/

router.beforeResolve((to, from, next) => {
  console.log("beforeResolve");
  next();
});

router.afterEach((to, from) => {
  console.log("afterEach");
});
